package com.py.framework.service.impl;

import java.sql.Timestamp;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import com.py.framework.common.Constants;
import com.py.framework.core.exception.ServiceException;
import com.py.framework.core.utils.DateUtils;
import com.py.framework.dao.MenuResourceDao;
import com.py.framework.entity.MenuEntity;
import com.py.framework.service.MenuResourceService;
import com.py.framework.service.RoleService;

/**
 * 菜单资源Service实现
 * @author Leegern
 * @date   2018年4月23日
 */
@Service
public class MenuResourceServiceImpl implements MenuResourceService {
	
	@Autowired
	private MenuResourceDao menuResourceDao;
	@Autowired
	private RoleService roleService;
	
	/*
	 * (non-Javadoc)
	 * @see com.py.framework.service.MenuResourceService#queryMenuResourceForPage(java.util.Map)
	 */
	@Override
	public List<MenuEntity> queryMenuResourceForPage(Map<String, Object> params) {
		return menuResourceDao.queryMenuResourceForPage(params);
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.py.framework.service.MenuResourceService#getMenuResourceByParentId(java.lang.String)
	 */
	@Override
	public List<MenuEntity> getMenuResourceByParentId(String parentId) {
		return menuResourceDao.getMenuResourceByParentId(parentId);
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.py.framework.service.MenuResourceService#getMenuResourceByMenuType(int)
	 */
	@Override
	public List<MenuEntity> getMenuResourceByMenuType(int menuType) {
		return menuResourceDao.getMenuResourceByMenuType(menuType);
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.py.framework.service.MenuResourceService#findOneMenuResource(long)
	 */
	@Override
	public MenuEntity findOneMenuResource(long id) {
		if (id > Constants.NUMBER_ZERO) {
			return menuResourceDao.findOneMenuResource(id);
		}
		return null;
	}

	@Override
	public MenuEntity getMenuNameByLinkURL(String linkUrl) {
		if (null != linkUrl && linkUrl.length() > Constants.NUMBER_ZERO) {
			return menuResourceDao.getMenuNameByLinkURL(linkUrl);
		}
		return null;
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.py.framework.service.MenuResourceService#addMenuResourceInfo(com.py.framework.entity.MenuEntity)
	 */
	@Transactional(propagation = Propagation.REQUIRED)
	@Override
	public int addMenuResourceInfo(MenuEntity entity) {
		if (null != entity) {
			if (entity.getParentId() > Constants.NUMBER_ZERO) {
				MenuEntity menu = this.findOneMenuResource(entity.getParentId());
				if (null != menu && StringUtils.isNotBlank(menu.getAllParentId())) {
					entity.setAllParentId(menu.getAllParentId() + "," + entity.getParentId());
				} else {
					entity.setAllParentId(String.valueOf(entity.getParentId()));
				}
			}
			entity.setCreateTime(new Timestamp(System.currentTimeMillis()));
			entity.setUpdateTime(new Timestamp(System.currentTimeMillis()));
			entity.setDelFlag(Constants.NUMBER_ONE);
			return menuResourceDao.addMenuResourceInfo(entity);
		}
		return 0;
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.py.framework.service.MenuResourceService#updateMenuResource(com.py.framework.entity.MenuEntity)
	 */
	@Transactional(propagation = Propagation.REQUIRED)
	@Override
	public int updateMenuResource(MenuEntity entity) {
		entity.setUpdateTime(new Timestamp(System.currentTimeMillis()));
		return menuResourceDao.updateMenuResource(entity);
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.py.framework.service.MenuResourceService#batchDelMenuResource(java.util.List)
	 */
	@Transactional(propagation = Propagation.REQUIRED)
	@Override
	public int batchDelMenuResource(List<Long> ids) {
		int flag = 0;
		if (! CollectionUtils.isEmpty(ids)) {
			Map<String, Object> params = new HashMap<String, Object>();
			List<MenuEntity> datas = null;
			for (Long id : ids) {
				datas = menuResourceDao.getMenuResourceByParentId(String.valueOf(id));
				if (! CollectionUtils.isEmpty(datas)) {
					throw new ServiceException("存在子节点，删除失败！");
				}
			}
			params.put("updateTime", new Timestamp(DateUtils.getCurrentDateTime().getTime()));
			params.put("ids",        ids);
			flag = menuResourceDao.batchDelMenuResource(params);
			// 删除角色菜单资源信息
			for (Long menuId : ids) {
				params.clear();
				params.put("menuId", menuId);
				roleService.delRoleMenuRelate(params);
			}
		}
		return flag;
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.py.framework.service.MenuResourceService#getUserMenuResources(long)
	 */
	@Override
	public List<MenuEntity> getUserMenuResources(long userId) {
		if (userId > Constants.NUMBER_ZERO) {
			return menuResourceDao.getUserMenuResources(userId);
		}
		return null;
	}
}